package pe.com.rix.taglib.view;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class Pager extends SimpleTagSupport {

	private String onPaging;
	private Integer total;
	private Integer current;
	
	@Override
	public void doTag() throws JspException, IOException {
		JspWriter out = getJspContext().getOut();

		if(!(getTotal()==null || getCurrent() == null)){
		
			String onPaging = this.getOnPaging();
			if(onPaging==null) onPaging =  "";
			
			int total = getTotal();
			int current = getCurrent();
			int totalGroup = 1;
			int currentGroup = 1;
			if(total>10){
				int compGroup = total/10;
				int incGroup = (total%10>0)?1:0;
				totalGroup = compGroup + incGroup;
				currentGroup = current/10 + ((current%10>0)?1:0);
			}
			
			System.out.println("Pagina " + getCurrent() + " de " + getTotal());
			System.out.println("Group: " + currentGroup + " de " + totalGroup);
			
			StringBuffer value = new StringBuffer();

			boolean anteriorG = false, siguienteG = false;
			
			if(currentGroup>1){
				value.append("<div class='unselected' onclick='" + onPaging.replaceAll("%p", String.valueOf(1)) + "'>") ;
				value.append("[Inicio]");
				value.append("</div>");
			}
			
			for(int g=0; g<totalGroup; g++){
				if(g+1==currentGroup){
					int cant = 0;
					if(currentGroup==totalGroup) cant = total;
					else cant = 10+(g*10);
					
					for(int i=1+(g*10);i<=cant; i++){
						if(i==current){
							value.append("<div class='selected'>");
							value.append(i);
							value.append("</div>");
						}else{
							value.append("<div class='unselected' onclick='" + onPaging.replaceAll("%p", String.valueOf(i)) + "'>");
							value.append(i);
							value.append("</div>");
						}
					}
				}else{
					if(g+1<currentGroup && anteriorG==false){
						value.append("<div class='unselected' onclick='" + onPaging.replaceAll("%p", String.valueOf((currentGroup-1)*10)) + "'>");
						value.append("[Anteriores 10]");
						value.append("</div>");
						anteriorG = true;
					}else if(g+1>currentGroup && siguienteG==false){
						value.append("<div class='unselected' onclick='" + onPaging.replaceAll("%p", String.valueOf(currentGroup*10+1)) + "'>");
						value.append("[Siguientes 10]");
						value.append("</div>");
						siguienteG = true;
					}
				}
			}
			
			if(currentGroup<totalGroup){
				value.append("<div class='unselected' onclick='" + onPaging.replaceAll("%p", String.valueOf( total )) + "'>");
				value.append("[Ultimo]");
				value.append("</div>");
			}
			
			
			try {
				JspFragment f = getJspBody();
				if (f != null) {
					f.invoke(out);
				}
				out.print(value.toString());
			} catch (java.io.IOException ex) {
				throw new JspException(ex.getMessage());
			}
		}
	}
	
	public Integer getTotal() {
		return total;
	}
	
	public Integer getCurrent() {
		return current;
	}
	
	public void setTotal(Integer total) {
		this.total = total;
	}
	public void setCurrent(Integer current) {
		this.current = current;
	}
	public void setOnPaging(String onPaging) {
		this.onPaging = onPaging;
	}
	public String getOnPaging() {
		return onPaging;
	}
	
}
